// pages/my_pet/my_pet.js
const app=getApp()
let img;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:['猫','狗','其他'],
    array_sex:['男','女'],
    array_inject:['否','是'],
    type:'',
    inputText:'',
    sex:'',
    age:'',
    weight:'',
    inject:'',
    content:''
  },

  bindinjectChange:function (e){
    console.log(e)
    this.setData({
      inject: e.detail.value,
      click_sex:''
    })
  },
  ageInput: function(e) {  /* 填写宠物年龄 */
    this.setData({
      age:e.detail.value
    })
  },
  bindSexChange: function(e){   /* 宠物性别 */
    this.setData({
      sex: e.detail.value,
      click_sex:''
    })
  },
  bindTypeChange: function(e) {  /* 宠物类型 */
    this.setData({
      type:e.detail.value
      // click_date:''
    })
  },

  onInputText(e) { /* 昵称 */
    this.setData({
      inputText: e.detail.value
    })
       
  },

 
 
  weightInput: function(e) {  /* 填写宠物体重 */
    this.setData({
      weight:e.detail.value
    })
 
  },

  contentInput: function(e) {   /* 内容补充 */
    this.setData({
      content:e.detail.value
    })
   
  },
  saveMessage: function(e) {  /* 立即发布 */

    
    wx.showLoading({
      title: '正在生成卡片',
    })
    
    setTimeout(function () {
      wx.hideLoading(),
      wx.navigateTo({
      url: '/pages/my_pet_publish/my_pet_publish',
    })
    }, 2000)
    this.uploadData();
  },
  chooseImg:function(){
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType:['original','compressed'],
      sourceType:['album','camera'],
      success(res){
        console.log(res.tempFilePaths[0])
        img = res.tempFilePaths[0]
        that.setData({
          Uimage:img
        })
      },
      fail(res){
        console.log('添加图片失败！')
      }
    })
  },
  uploadData:function() {  /* 将用户信息上传到 数据库 */
    let value = this.data;
    console.log(img)
    wx.request({
      url: `${app.globalData.url}/users/my-pet-publish`,//写自己的服务器
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      data: {
        Pid:this.data.Pid,
        Uid:wx.getStorageSync('Uid'),
        Pname:value.inputText,
        Psex:value.sex,
        Page:value.age,
        Pweight:value.weight,
        Pvarie:value.type,
        Photo:img,
        Pinject:value.inject,
        Pintroduction:value.content
      },
      success: function () { 
       
        console.log("success") },
      fail: function (res) {
         console.log("fail")
         console.log(res)
      }
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.importData();
    this.data.Pid=options.Pid
  },
  importData: function(){/* 查询数据 */
    console.log(this.data)
    let that = this;
    console.log(that.data)
    wx.request({   
      url: `${app.globalData.url}/users/my-pet-publish`,
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        Uid:wx.getStrorageSync('Uid')
      },
    
      success: function(res) {
      //这里就是请求成功后，进行一些函数操作
      let pet = {
      Pid:'10001',
      Pname:'汪汪',
      Psex:0,
      Page:'3个月',
      Pweight:'2',
      Pvarie:'金毛',
      Photo:'/images/cat2 (3).jpg',
      Pinject:1,
      Pintroduction:'汪汪喜欢吃牛肉和胡萝卜，不能吃巧克力和夏威夷果；喜欢的玩具是毛绒恐龙'
    }
        var data1 = res.data[0];
        console.log(that.data.sex)
        // that.setData({
        //   Pid:'20001',
        //   "that.data.inputText":data1.Pname,
        //   "data.sex":data1.Psex,
        //   "that.data.age":data1.Page,
        //   "that.data.weight":data1.Pweight,
        //   "that.data.type":data1.Pvarie,
        //   "that.data.Photo":data1.Photo,
        //   "that.data.inject":data1.Pinject,
        //   "that.data.introduction":data1.Pintroduction
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})

